/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.ExpressDao;
import com.taobao.snowsnow.hibernate.pojo.Express;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ExpressDaoImpl extends BaseDaoImpl implements ExpressDao {
	
	public Express findById(int id) throws Exception {
		return (Express) getHibernateTemplate().get(Express.class, id);
	}
	
	public List<Express> findByName(String name) throws Exception {
		return super.findByProperty(Express.class, "name", name);
	}

	public List<Express> findAll() throws Exception {
		return getHibernateTemplate().find("from Express");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Express.class);
	}

	public List<Express> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Express.class, page, limit);
	}

	@Override
	public Express getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Express as o where o.id = (select min(id) from Express)");
		List<Express> list = query.list();
		if(list.size() !=0)
			return (Express) list.get(0);
		else return null;
	}

	@Override
	public Express getLastObject() throws Exception {
		Query query = getSession().createQuery("from Express as o where o.id = (select max(id) from Express)");
		List<Express> list = query.list();
		if(list.size() !=0)
			return (Express) list.get(0);
		else return null;
	}

	@Override
	public Express findByExpressCode(String code) throws Exception {
		Query query = getSession().createQuery("from Express as o where o.codeName = :codeName");
		query.setString("codeName", code);
		List<Express> list = query.list();
		if(list.size() !=0)
			return (Express) list.get(0);
		else return null;
	}
}
